/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2008，所有权利保留。
 * 
 * 项目名：	rumba
 * 文件名：	TBButtonCmpst.java
 * 模块说明：	
 * 修改历史：
 * Jan 23, 2008 - lxm - 创建。
 */
package com.hd123.rumba.gwt.entry.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.Widget;

/**
 * 工具栏按钮 | 界面部件
 * 
 * @author lxm
 * 
 */
class TBButtonCmpst extends Composite {

  /**
   * 构造函数
   * 
   * @param leftEdge
   *          指定左边是否是边缘。
   * @param rightEdge
   *          指定右边是否是边缘。
   */
  public TBButtonCmpst(boolean leftEdge, boolean rightEdge) {
    super();
    this.leftEdge = leftEdge;
    this.rightEdge = rightEdge;
    drawSelf();
  }

  protected void doClick() {
    // Do Nothing
  };

  // --------------------------------------------------------------------------
  // 数据
  // --------------------------------------------------------------------------
  
  private final static String DEFAULT_ICONURL = "images/buttons/default.gif";

  private boolean leftEdge;
  private boolean rightEdge;
  private String caption;
  private boolean enabled = true;
  private boolean hot = false;
  private boolean down = false;
  private String iconUrl;
  private String hotIconUrl;
  private String downIconUrl;
  private String disabledIconUrl;

  /** 标题 */
  public String getCaption() {
    return caption;
  }

  /** 标题 */
  public void setCaption(String caption) {
    this.caption = caption;
    refresh();
  }

  /** 不可用状态的前景图标 */
  public String getDisabledIconUrl() {
    return disabledIconUrl == null ? iconUrl : disabledIconUrl;
  }

  /** 不可用状态的前景图标 */
  public void setDisabledIconUrl(String disabledIconUrl) {
    this.disabledIconUrl = disabledIconUrl;
    refresh();
  }

  /** 按钮按下时的前景图标 */
  public String getDownIconUrl() {
    return downIconUrl == null ? iconUrl : downIconUrl;
  }

  /** 按钮按下时的前景图标 */
  public void setDownIconUrl(String downIconUrl) {
    this.downIconUrl = downIconUrl;
    refresh();
  }

  /** 热状态的前景图标 */
  public String getHotIconUrl() {
    return hotIconUrl == null ? iconUrl : hotIconUrl;
  }

  /** 热状态的前景图标 */
  public void setHotIconUrl(String hotIconUrl) {
    this.hotIconUrl = hotIconUrl;
    refresh();
  }

  /** 前景图标 */
  public String getIconUrl() {
    return iconUrl == null ? DEFAULT_ICONURL : iconUrl;
  }

  /** 前景图标 */
  public void setIconUrl(String iconUrl) {
    this.iconUrl = iconUrl;
    refresh();
  }

  /** 是否处于可用状态 */
  public boolean isEnabled() {
    return enabled;
  }

  /** 是否处于可用状态 */
  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
    refresh();
  }

  private void setDown(boolean down) {
    this.down = down;
    refresh();
  }

  private void setHot(boolean hot) {
    this.hot = hot;
    refresh();
  }

  // --------------------------------------------------------------------------
  // 用户界面
  // --------------------------------------------------------------------------

  private HorizontalPanel pnl;
  private Image img;

  private class Listener_img implements MouseListener, ClickListener {

    public void onMouseDown(Widget sender, int x, int y) {
      setDown(true);
    }

    public void onMouseEnter(Widget sender) {
      setHot(true);
    }

    public void onMouseLeave(Widget sender) {
      setHot(false);
    }

    public void onMouseMove(Widget sender, int x, int y) {
      // Do Nothing
    }

    public void onMouseUp(Widget sender, int x, int y) {
      setDown(false);
    }

    public void onClick(Widget sender) {
      doClick();
    }

  }

  private void drawSelf() {
    pnl = new HorizontalPanel();
    this.setStyleAttrs_pnl();

    img = new Image();
    DOM.setStyleAttribute(img.getElement(), "cursor", "hand");
    DOM.setStyleAttribute(img.getElement(), "paddingTop", "1px");
    // img.setStyleName(this.getStyleName_img());
    Listener_img lstn = new Listener_img();
    img.addClickListener(lstn);
    img.addMouseListener(lstn);

    pnl.add(img);
    pnl.setCellHorizontalAlignment(img, HorizontalPanel.ALIGN_CENTER);
    pnl.setCellVerticalAlignment(img, HorizontalPanel.ALIGN_MIDDLE);
    this.initWidget(pnl);
  }

  /** 刷新按钮的显示 */
  private void refresh() {
    assert img != null;

    img.setTitle(caption);

    DOM.setStyleAttribute(pnl.getElement(), "backgroundImage", "url(\"" + getBgImageUrl() + "\")");

    if (!this.enabled)
      img.setUrl(this.getDisabledIconUrl());
    else {
      if (this.hot)
        img.setUrl(this.down ? this.getDownIconUrl() : this.getHotIconUrl());
      else
        img.setUrl(this.getIconUrl());
    }
  }

  private void setStyleAttrs_pnl() {
    pnl.setHeight("19px");
    pnl.setWidth(this.rightEdge ? "22px" : "21px");
  }

  private String getBgImageUrl() {
    StringBuffer sb = new StringBuffer();
    sb.append("images/buttons/btn-bg-");
    sb.append(this.leftEdge ? "1" : "0");
    sb.append(this.rightEdge ? "1" : "0");
    if (!this.enabled)
      sb.append("-dis");
    else {
      if (this.hot)
        sb.append(this.down ? "-down" : "-hot");
    }
    sb.append(".gif");
    return sb.toString();
  }
}
